﻿using MvvmCross.Platform;
using MvvmCross.Plugins.Messenger;
using MvvmCross.Core.ViewModels;
using Sidvall.LogManager.UI.Messaging;
using System.Windows.Input;

namespace Sidvall.LogManager.UI.ViewModels
{
	public class NavigationViewModel : Sidvall.UI.ViewModels.ViewModelBase
	{
		#region Public Members

		#region CanShowStart

		private bool _CanShowStart;
		public bool CanShowStart
		{
			get
			{
				return _CanShowStart;
			}
			set
			{
				_CanShowStart = value;
				RaisePropertyChanged("CanShowStart");
			}
		}

		#endregion
		#region CanShowLogScopes

		private bool _CanShowLogScopes;
		public bool CanShowLogScopes
		{
			get
			{
				return _CanShowLogScopes;
			}
			set
			{
				_CanShowLogScopes = value;
				RaisePropertyChanged("CanShowLogScopes");
			}
		}

		#endregion
		#region CanShowResourceItems

		private bool _CanShowResourceItems;
		public bool CanShowResourceItems
		{
			get
			{
				return _CanShowResourceItems;
			}
			set
			{
				_CanShowResourceItems = value;
				RaisePropertyChanged("CanShowResourceItems");
			}
		}

		#endregion
		#region ShowLogScopesCommand

		public ICommand ShowLogScopesCommand
		{
			get
			{
				return new MvxCommand(() => ShowViewModel<LogScopeViewModel>());
			}
		}

		#endregion

		#region Start

		public override void Start()
		{
			var messenger = Mvx.Resolve<IMvxMessenger>();
			this.ViewModelResources.Add(messenger.Subscribe<ServerManagerMessage>(OnServerManagerMessage));
			InitializeCommands();
		}

		#endregion

		#endregion
		#region Private  Members

		#region InitializeCommands

		private void InitializeCommands()
		{
			if (Sidvall.LogManager.UI.SystemContext.Current.ServerManagers == null)
			{
				this.CanShowStart = false;
				this.CanShowLogScopes = false;
				this.CanShowResourceItems = false;
			}
			else if (Sidvall.LogManager.UI.SystemContext.Current.ServerManagers.IsValid)
			{
				this.CanShowStart = true;
				this.CanShowLogScopes = true;
				this.CanShowResourceItems = true;
			}
			else
			{
				this.CanShowStart = false;
				this.CanShowLogScopes = false;
				this.CanShowResourceItems = false;
			}
		}

		#endregion

		#region OnServerManagerMessage

		private void OnServerManagerMessage(ServerManagerMessage item)
		{
			InitializeCommands();
		}

		#endregion

		#endregion
	}
}
